package com.easycms.web.system.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.easycms.framework.domain.ResultAdaptDto;
import com.easycms.framework.properties.EasyCmsConfig;
import com.easycms.framework.shiro.UserOwnInfo;
import com.easycms.framework.web.ActionResult;
import com.easycms.framework.web.BaseController;
import com.easycms.framework.web.RequestHelper;
import com.easycms.web.system.domain.SysMenu;
import com.easycms.web.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.easycms.web.system.service.*;
import java.util.List;


/**
*
 * @author yushuo
**/
@Controller
@RequestMapping(value={"/","/home"})
public class HomeController extends BaseController {
    @Autowired
    private SysMenuService menuService;
    @Autowired
    private SysUserService userService;
    @Autowired
    private SysRoleService roleService;
    @Autowired
    private EasyCmsConfig easyCmsConfig;
    @Autowired
    private RequestHelper requestHelper;

    @GetMapping(value={"","/index"})
    public ActionResult index(Model model){

        UserOwnInfo userOwnInfo=requestHelper.getUserOwnInfo();
        List<SysMenu> menus;
        if(userOwnInfo.isAdmin()){
            menus=menuService.getAllMenus();
        }else{
            menus=roleService.getPermissionMenus(userOwnInfo.getRoleId());
        }

        model.addAttribute("nowTime", DateUtil.now());
        model.addAttribute("user",userOwnInfo);
        model.addAttribute("menus",menus);
        model.addAttribute("demoEnabled",true);
        model.addAttribute("ignoreFooter",true);
        model.addAttribute("projectName",easyCmsConfig.getName());
        model.addAttribute("copyrightYear",easyCmsConfig.getCopyrightYear());
        model.addAttribute("systemVersion",easyCmsConfig.getVersion());
        System.out.println(menus);
        return view("home/index");
    }

    @GetMapping("main")
    public ActionResult main(){
        return view();
    }

    @GetMapping("lock")
    public ActionResult lock(Model model){
        UserOwnInfo userOwnInfo=requestHelper.getUserOwnInfo();
        model.addAttribute("user",userOwnInfo);
        return view();
    }

    /**
    *
     * @author yushuo
     * @description 解锁
     * @date 10:24 2021/3/1
     * @param []
     * @return com.easycms.framework.web.ActionResult
    **/
    @GetMapping("unlock")
    public ActionResult unlock(){
        String password=requestHelper.getString("password","");
        if(StrUtil.isEmpty(password)){
            return error("用户密码错误");
        }

        UserOwnInfo userOwnInfo= requestHelper.getUserOwnInfo();
        SysUser user=userService.getModel(userOwnInfo.getId());
        if(user==null){
            return error("用户密码错误");
        }
        if(!user.getPassWord().equals(password)){
            return error("用户密码错误");
        }
            return ok();
    }
    /**
    *
     * @author yushuo
     * @description //切换主题皮肤
    **/
    @GetMapping("/system/switchSkin")
    public ActionResult switchSkin()
    {
        return view("home/skin");
    }
}
